import request from './request';
import video1 from "@/videos/video1.mp4";
import video2 from "@/videos/video2.mp4";
import video3 from "@/videos/video3.mp4";
import video4 from "@/videos/video4.mp4";
import video5 from "@/videos/video5.mp4";
import video6 from "@/videos/video6.mp4";
import video10 from "@/videos/video10.mp4";
import gif1 from "@/videos/1.gif";//默认
import gif2 from "@/videos/2.gif";//孙涛
import gif3 from "@/videos/3.gif";//李璐
import gif4 from "@/videos/4.gif";//卓尔
import gif5 from "@/videos/5.gif";//都琳
import gif6 from "@/videos/6.gif";//董琳雅
import gif10 from "@/videos/10.gif";//欣慰

/**
 * get请求
 * @param params 请求参数
 * @param args 其他请求参数
 * @return Promise
 * */
export function get(url, params = {}){
    Object.assign(params, {visitsrc: 'webpc'})
    return request.get(url, {params})
}

/**
 * post请求
 * @param params 请求参数
 * @param args 其他请求参数
 * @return Promise
 * */
export function post(url, params = {}){
    Object.assign(params, {visitsrc: 'webpc'})
   return request.post(url, params)
}



/**
 * @ele 需要显示视频的元素
 * @src 视频资源地址
 *
 * */
export class Speakvideo {
    constructor(ele, timetype, fn) {
        this.timedata = { // currentTime 设置列表
            1: {endtime: 11.6, endspeaktime: 11.4, enddetermine1: 11, enddetermine2: 18.9, src: video1, top: '0px'}, // 短头发
            2: {endtime: 12, endspeaktime: 12, enddetermine1: 12, enddetermine2: 16.7, src: video2, top: '-115px'}, // 孙涛
            3: {endtime: 12, endspeaktime: 12, enddetermine1: 12, enddetermine2: 16.7, src: video3, top: '0px'}, // 李璐
            4: {endtime: 12, endspeaktime: 12, enddetermine1: 12, enddetermine2: 16.7, src: video4, top: '-85px'}, // 卓尔
            5: {endtime: 12, endspeaktime: 12, enddetermine1: 12, enddetermine2: 16.7, src: video5, top: '-120px'}, // 都琳
            6: {endtime: 12, endspeaktime: 12, enddetermine1: 12, enddetermine2: 16.7, src: video6, top: '-90px'}, // 董琳雅
            10: {endtime: 12, endspeaktime: 12, enddetermine1: 12, enddetermine2: 16.7, src: video10, top: '-80px'}, //欣慰
        }
        var gifs = {
            1: gif1,
            2: gif2,
            3: gif3,
            4: gif4,
            5: gif5,
            6: gif6,
            10: gif10,
        }

        this.typeTimeData = this.timedata[timetype || 1]
        this.gif = gifs[timetype || 1]
        this.status = 2  //  1 说话 ; 2 不说话
        ele.innerHTML = `<video id="video_abcdefg" class="${[2,3,4,5,6,10].indexOf(timetype) > -1 && 'scale'}" preload="auto" x5-video-player-type="h5-page"  webkit-playsinline="true" playsinline="true" x5-video-orientation="landscape|portrait" autoplay muted loop style="width: 100%;"><source src="${this.typeTimeData.src}" type="video/mp4"></video>`
        this.video = document.querySelector('#video_abcdefg')
        this.video.load()
        this.video.canpaly = () => {
            fn && fn()
        }
        document.addEventListener('click', () => {
            this.video.play()
        })
        document.addEventListener('touchstart', () => {
            this.video.play()
        })
        document.addEventListener("WeixinJSBridgeReady", () => {
            this.video.play()
        }, false);
        this.video.addEventListener('timeupdate', (e) => {
            if (this.status === 1) {
                // 说话
                if (e.target.currentTime > this.typeTimeData.enddetermine1) {
                    this.video.currentTime = 0
                }
            } else {
                // 不说话
                if (e.target.currentTime < this.typeTimeData.enddetermine1 || e.target.currentTime > this.typeTimeData.enddetermine2) {
                    this.video.currentTime = this.typeTimeData.endtime
                }
            }
        })
    }
    // 开始说话
    startSpeak() {
        this.status = 1
        this.video.currentTime = 0
    }
    // 结束说话
    endSpeak() {
        this.status = 2
        this.video.currentTime = this.typeTimeData.endspeaktime
    }
}

export function getParams(type) {
    var local = window.location, params = '';
    if (local.search && local.search.length > 4) {
        if (type === 'get') {
            return local.search.substr(1)
        } else {
            params = {};
            var str = local.search.substr(1), i, item;
            str = str.split('&');
            if (Array.isArray(str) && str.length > 0) {
                for (i in str) {
                    if (str[i] && (typeof str[i] === 'string')) {
                        item = str[i].split('=');
                        params[item[0]] = item[1];
                    }
                }
            }
        }
    }
    return params;
}

export function UUID(){
    let hx='0123456789abcde0123456789abcde0123456789abcde'.split('');
    let len=32,h='',he=hx.length;
    let now=new Date().getTime(),n2=''+now,l2=n2.length,tarlen=len-l2;
    for (let i = 0; i < tarlen; i++) {
        h+=hx[parseInt(he*Math.random())];
    }
    return n2+h;
}
export const icalling = {
    tmICalling: null,
    start(fn){
        var c = 0;
        this.tmICalling = setInterval((function () {
            fn && fn('...'.substring(0, c % 4))
            c++;
        }), 500);
    },
    stop(){
        if(this.tmICalling){
            clearTimeout(this.tmICalling)
        }
    }
}

export function getDevice(){
    function is360() {
        let up = navigator.plugins
        let is360B = false
        for (let key in up) {
            if(up[key] && up[key].name == 'QQÒôÀÖ²¥·Å¿Ø¼þ'){
                is360B = true
            }
        }
        return is360B
    }
    let n = navigator
    let u = n.userAgent

    let device = ''; // windows, mac, ipad, ios, android
    let browser = '';//
    let system = '';//
    let browserName = {
        WeChat: '微信浏览器',
        Chrome: '谷歌浏览器',
        Safari: 'Safari浏览器',
        QQ: 'QQ浏览器',
        UC: 'UC浏览器',
        360: '360浏览器'
    }

    if (/(Win32|Win64)/i.test(n.platform) || /(Windows|Win64|x64)/i.test(u)) {
        device = 'windows'
    }
    if (/(MacIntel|Mac)/i.test(n.platform) || /Macintosh;.*?Mac OS/i.test(u)) {
        device = 'mac'
    }
    if (/(iPad)/i.test(n.platform) || /iPad;.*?Mac OS/i.test(u)) {
        device = 'ipad'
    }
    if (/(iPhone)/i.test(n.platform) || /iPhone;.*?Mac OS/i.test(u)) {
        device = 'iphone'
    }
    if (/(Linux|aarch64)/i.test(n.platform) || /Android|android|Linux/i.test(u)) {
        device = 'android'
    }

    if (/(MicroMessenger|WeChat|Weixin)/i.test(u)) {
        browser = 'WeChat'
    }
    if (/(QQ)/i.test(u) && /(MQQBrowser)/i.test(u)) {
        browser = 'QQ'
    }
    if (/(UCBrowser)/i.test(u)) {
        browser = 'UC'
    }
    if (/(MicroMessenger|WeChat|Weixin)/i.test(u)) {
        browser = 'WeChat'
    }
    let u_Chrome = /(Chrome)/i.test(u)
    let u_Safari = /(Safari)/i.test(u)
    let u_Version = /(Version)/i.test(u)
    if (u_Chrome && u_Safari && !u_Version) {
        browser = 'Chrome'
    }
    if (!u_Chrome && u_Safari && u_Version) {
        browser = 'Safari'
    }

    if(is360()){
        browser = 360
    }

    let version = u.match(/(Safari|Chrome).*?[0-9.]+/)
    if (version) {
        version = version[0]
    }
    if(['windows'].indexOf(device) > -1){
        system = 'windows'
    }
    if(['mac', 'ipad', 'iphone'].indexOf(device) > -1){
        system = 'ios'
    }
    return {
        device,
        system,
        browser,
        version,
        browserName: browserName[browser],
        language: n.language,
        onLine: n.onLine
    }
}
